package com.yx.yinshanjinniu.fragments;

import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.yx.yinshanjinniu.R;
import com.yx.yinshanjinniu.activitys.BeforeRechargeActivity;
import com.yx.yinshanjinniu.activitys.EditNickNameActivity;
import com.yx.yinshanjinniu.activitys.PointsMallActivity;
import com.yx.yinshanjinniu.activitys.SignActivity;
import com.yx.yinshanjinniu.activitys.TakeMoneyActivity;
import com.yx.yinshanjinniu.adapters.WelfareAdapter;
import com.yx.yinshanjinniu.base.BaseActivity;
import com.yx.yinshanjinniu.base.BaseFragment;
import com.yx.yinshanjinniu.base.BaseRVAdapter;
import com.yx.yinshanjinniu.bean.GifBean;
import com.yx.yinshanjinniu.bean.UserInfoBean;
import com.yx.yinshanjinniu.bean.WelfareTaskBean;
import com.yx.yinshanjinniu.eventbus.EventCenter;
import com.yx.yinshanjinniu.eventbus.EventConstants;
import com.yx.yinshanjinniu.net.ActionCallbackListener;
import com.yx.yinshanjinniu.utils.DataUtils;
import com.yx.yinshanjinniu.utils.GlideUtils;
import com.yx.yinshanjinniu.utils.NoScrollLayoutManager;
import com.yx.yinshanjinniu.utils.RouteUtil;
import com.yx.yinshanjinniu.utils.ToastUtil;
import com.yx.yinshanjinniu.utils.Utils;
import com.yx.yinshanjinniu.view.CertificateModule;
import com.yx.yinshanjinniu.view.SignDialog;
import com.zc.mychart.model.GoodsAllBean;

import java.util.ArrayList;
import java.util.List;

public class WelfareFragment extends BaseFragment implements View.OnClickListener {

    private static final String TAG = "WelfareFragment";
    private RecyclerView tenday_rv, day_rv, week_rv;
    private LinearLayout week_ll, day_ll, tenday_ll;
    private WelfareAdapter tendayAdapter, dayAdapter, weekAdapter;
    private List<WelfareTaskBean.DayBean> tendayList, dayList, weekList;
    private TextView h_integral, signTitle, signImg;
    private ImageView welfareExchange, welfare_gif;
    private SignDialog mSignDialog;

    @Override
    protected void initVariable() {
        tendayList = new ArrayList<WelfareTaskBean.DayBean>();
        dayList = new ArrayList<WelfareTaskBean.DayBean>();
        weekList = new ArrayList<WelfareTaskBean.DayBean>();
        tenday_rv.setLayoutManager(new NoScrollLayoutManager(mContext));
        day_rv.setLayoutManager(new NoScrollLayoutManager(mContext));
        week_rv.setLayoutManager(new NoScrollLayoutManager(mContext));
        tendayAdapter = new WelfareAdapter(mContext, tendayList);
        tenday_rv.setAdapter(tendayAdapter);
        dayAdapter = new WelfareAdapter(mContext, dayList);
        day_rv.setAdapter(dayAdapter);
        weekAdapter = new WelfareAdapter(mContext, weekList);
        week_rv.setAdapter(weekAdapter);


        tendayAdapter.setOnItemClickListener(new BaseRVAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(List data, View view, int position, long id) {
                WelfareTaskBean.DayBean dayBean = (WelfareTaskBean.DayBean) data.get(position);
                itemClick(dayBean);
            }
        });
        dayAdapter.setOnItemClickListener(new BaseRVAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(List data, View view, int position, long id) {
                WelfareTaskBean.DayBean dayBean = (WelfareTaskBean.DayBean) data.get(position);
                itemClick(dayBean);
            }
        });
        weekAdapter.setOnItemClickListener(new BaseRVAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(List data, View view, int position, long id) {
                WelfareTaskBean.DayBean dayBean = (WelfareTaskBean.DayBean) data.get(position);
                itemClick(dayBean);
            }
        });
    }

    private void itemClick(WelfareTaskBean.DayBean dayBean) {
        Log.e("WelfareFragment", "onItemClick: " + dayBean.getTitle());
        if (dayBean.getTaskStatus().equals("0")) {//去完成
//            typeImg.setImageResource(R.drawable.welfare_finish);
            if (dayBean.getLink().equals("app://toSaving")) {
                gotoActivity(mContext, BeforeRechargeActivity.class, null);
                return;
            } else if (dayBean.getLink().equals("app://toOrder")) {
                Log.e(TAG, "itemClick: " + "");
                List<GoodsAllBean> allList = myApplication.getAllList();
                if (null != allList && allList.size() > 0) {
                    RouteUtil.toMarketActivity(baseActivity, allList.get(0).pro_code);
                } else {
                    ToastUtil.showToast(mContext, "暂未获取商品信息");
                }
            } else if (dayBean.getLink().equals("app://toUser")){
                RouteUtil.gotoActivity(mContext, EditNickNameActivity.class, null);
            } else if (dayBean.getLink().equals("app://toWithdrawal")){
                if (CertificateModule.getInstance(mContext).isCertificate(null)) {
                    gotoActivity(mContext, TakeMoneyActivity.class, null);
                }
            }
        } else if (dayBean.getTaskStatus().equals("1")) {//领取任务
//            typeImg.setImageResource(R.drawable.welfare_pickup);
            tasksGetIntegral(dayBean);
        } else if (dayBean.getTaskStatus().equals("2")) {//已领取
            return;
        }
    }

    private void tasksGetIntegral(WelfareTaskBean.DayBean dayBean) {
        mAppAction.getTaskszIntegral(String.valueOf(dayBean.getId()), String.valueOf(dayBean.getTaskId()), new ActionCallbackListener<Void>() {
            @Override
            public void onSuccess(Void data) {
                ToastUtil.showToast(mContext, "领取成功");
                refreshDataSource();
            }

            @Override
            public void onFailure(String errorCode, String message) {
                ToastUtil.showToast(mContext, message);
            }
        });

    }

    @Override
    protected int setContentView() {
        return R.layout.fragment_welfare;
    }

    @Override
    protected void findViews(View rootView) {
        tenday_ll = (LinearLayout) $(R.id.tenday_ll);
        day_ll = (LinearLayout) $(R.id.day_ll);
        week_ll = (LinearLayout) $(R.id.week_ll);
        tenday_rv = (RecyclerView) $(R.id.welfare_tenday_rv);
        day_rv = (RecyclerView) $(R.id.welfare_day_rv);
        week_rv = (RecyclerView) $(R.id.welfare_week_rv);
        signTitle = (TextView) $(R.id.welfare_sign_title);
        h_integral = (TextView) $(R.id.welfare_integral_text);
        signImg = (TextView) $(R.id.welfare_sign);
        welfareExchange = (ImageView) $(R.id.welfare_exchange);
        welfare_gif = (ImageView) $(R.id.welfare_gif);
    }

    @Override
    protected void setListeners() {
        signImg.setOnClickListener(this);
        welfare_gif.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RouteUtil.routePageByUrl((BaseActivity) mContext, myApplication.getGifMap().get("3").link);
            }
        });
        welfareExchange.setOnClickListener(this);
        GifBean gifBean = myApplication.getGifMap().get("3");
        if (gifBean == null || TextUtils.isEmpty(gifBean.img)) {
            welfare_gif.setVisibility(View.GONE);
        } else {
            welfare_gif.setVisibility(View.VISIBLE);
            GlideUtils.showImageFitxyOptions(mContext, gifBean.img, welfare_gif);
        }
    }

    @Override
    protected void refreshDataSource() {
        if (DataUtils.isLogin(mContext)) {
            signImg.setEnabled(true);
            loadWelfareData();
            UserInfoBean bean = DataUtils.getUserInfo(mContext);
            h_integral.setText("我的积分： " + bean.integral);
            if (bean.isSign == 0) {
//                signImg.setImageResource(R.drawable.welfare_sign_s);
                signTitle.setText("连续签到，领大额积分");

            } else {
//                signImg.setImageResource(R.drawable.welfare_sign_l);
                signTitle.setText("今日签到积分已到账");
            }
        } else {
//            signImg.setImageResource(R.drawable.welfare_sign_s);
            signTitle.setText("连续签到，领大额积分");
        }
        String week = Utils.getWeek(System.currentTimeMillis());
        if (week.contains("周六") || week.contains("周日")) {
//            signImg.setImageResource(R.drawable.welfare_sign_n);
            signImg.setEnabled(false);
        }

    }

    private void loadWelfareData() {
        mAppAction.getWelfareList(new ActionCallbackListener<WelfareTaskBean>() {
            @Override
            public void onSuccess(WelfareTaskBean data) {
                if (data.getNewX() != null) {
                    tendayList.clear();
                    tendayList.addAll(data.getNewX());
                    if (tendayList.size() > 0) {
                        tenday_ll.setVisibility(View.VISIBLE);
                        tendayAdapter.notifyDataSetChanged();
                    } else {
                        tenday_ll.setVisibility(View.GONE);
                    }
                } else {
                    tenday_ll.setVisibility(View.GONE);
                }
                if (data.getDay() != null) {
                    dayList.clear();
                    dayList.addAll(data.getDay());
                    if (dayList.size() > 0) {
                        day_ll.setVisibility(View.VISIBLE);
                        dayAdapter.notifyDataSetChanged();
                    } else {
                        day_ll.setVisibility(View.GONE);
                    }
                } else {
                    day_ll.setVisibility(View.GONE);
                }
                if (data.getWeek() != null) {
                    weekList.clear();
                    weekList.addAll(data.getWeek());
                    if (weekList.size() > 0) {
                        week_ll.setVisibility(View.VISIBLE);
                        weekAdapter.notifyDataSetChanged();
                    } else {
                        week_ll.setVisibility(View.GONE);
                    }
                } else {
                    week_ll.setVisibility(View.GONE);
                }
            }

            @Override
            public void onFailure(String errorCode, String message) {

            }
        });
    }

    @Override
    protected void onEventComming(EventCenter eventCenter) {
        super.onEventComming(eventCenter);
        switch (eventCenter.getEventCode()) {
            case EventConstants.EVENTCODE_UPDATE_USER://更新数据
                refreshDataSource();
                break;
        }
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.welfare_sign) {
            UserInfoBean bean = DataUtils.getUserInfo(mContext);
//            showSignDailog();

            gotoActivity(mContext, SignActivity.class, null);
//            if (bean.isSign == 0) {//未签到
//                userSign();
//            } else {//已签到
//                showSignDailog();
//            }
            return;
        }
        if (view.getId() == R.id.welfare_exchange) {
            gotoActivity(mContext, PointsMallActivity.class, null);
            return;
        }
    }

    private void showSignDailog() {
        if (null == mSignDialog) {
            mSignDialog = new SignDialog(mContext, true, Gravity.BOTTOM);
        }
        if (!mSignDialog.isShowing()) {
            mSignDialog.show();
        }
    }

    private void userSign() {
        mAppAction.signPost(new ActionCallbackListener<Void>() {
            @Override
            public void onSuccess(Void data) {//更新用户数据
                ToastUtil.showToast(mContext, "签到成功");
                myApplication.updateUserDetail();
            }

            @Override
            public void onFailure(String errorCode, String message) {
                ToastUtil.showToast(mContext, message);
            }
        });
    }
}
